<template>
<div class="dropdown" ref="dropDownRef">
  <a href="#" class="btn btn-outline-light my-2 dropdown-toggle" @click.prevent="changeToggleOpen">{{title}}</a>
  <ul class="dropdown-menu" :style="{ display: isToggleOpen ? 'block' : 'none'}" >
    <slot></slot>
  </ul>
</div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import useClickOutside from '../hooks/useClickOutside'
export default defineComponent({
  name: 'DropDown',
  props: {
    title: {
      type: String,
      required: true
    }
  },
  setup () {
    const isToggleOpen = ref(false)
    const dropDownRef = ref<null | HTMLElement>(null)
    const changeToggleOpen = () => {
      isToggleOpen.value = !isToggleOpen.value
    }
    // 判断是否点击的外部
    const isClickOutside = useClickOutside(dropDownRef)
    watch(isClickOutside, () => {
      if (isToggleOpen.value && isClickOutside.value) {
        isToggleOpen.value = false
      }
    })
    return {
      isToggleOpen,
      changeToggleOpen,
      dropDownRef
    }
  }
})
</script>

<style>

</style>
